package org.mspring.mlog.service;

import java.util.Date;

import org.mspring.mlog.dao.PostDao;
import org.mspring.mlog.entity.Post;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.query.QueryCriterion;
import org.mspring.platform.utils.StringUtils;
import org.mspring.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @author Gao Youbo
 * @since 2014-01-11
 */
@Service
public class PostService {
    @Autowired
    private PostDao postDao;

    public Post createPost(Post post) {
        if (post.getCreateTime() == null) {
            post.setCreateTime(new Date());
        }
        if (StringUtils.isBlank(post.getCommentStatus())) {
            post.setCommentStatus(Post.CommentStatus.OPEN);
        }
        if (StringUtils.isBlank(post.getStatus())) {
            post.setStatus(Post.Status.PUBLISH);
        }
        if (post.getReplys() == null) {
            post.setReplys(new Long(0));
        }
        if (post.getIsTop() == null) {
            post.setIsTop(false);
        }
        if (post.getAuthor() == null) {
            post.setAuthor(SecurityUtils.getCurrentUser());
        }
        post.setReplys(new Long(0));
        post.setViews(new Long(0));
        return postDao.merge(post);
    }

    public Post get(Long id) {
        return postDao.get(id);
    }

    public Page<Post> listPage(QueryCriterion queryCriterion, Page<Post> postPage) {
        return postDao.listPage(queryCriterion, postPage);
    }

    public void update(Post post) {
        postDao.update(post);
    }

    public void remove(Long... id) {
        postDao.remove(id);
    }

    public void remove(Post post) {
        postDao.remove(post);
    }
}
